package com.weai.portal.service.impl;

import com.weai.portal.entity.Solution;
import com.weai.portal.repository.SolutionRepository;
import com.weai.portal.service.SolutionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SolutionServiceImpl implements SolutionService {

    @Autowired
    private SolutionRepository solutionRepository;

    @Override
    public List<Solution> getAllSolutions() {
        log.debug("开始获取所有解决方案");
        try {
            List<Solution> solutions = solutionRepository.findAll();
            log.info("成功获取所有解决方案，数量: {}", solutions.size());
            return solutions;
        } catch (Exception e) {
            log.error("获取所有解决方案失败", e);
            throw e;
        }
    }

    @Override
    public List<Solution> searchSolutions(String keyword) {
        log.debug("开始搜索解决方案，关键词: {}", keyword);
        try {
            List<Solution> solutions = solutionRepository.searchByKeyword(keyword);
            log.info("成功搜索解决方案，关键词: {}，数量: {}", keyword, solutions.size());
            return solutions;
        } catch (Exception e) {
            log.error("搜索解决方案失败，关键词: {}", keyword, e);
            throw e;
        }
    }

    @Override
    public Solution getSolutionById(Integer id) {
        log.debug("开始获取解决方案详情，ID: {}", id);
        try {
            Solution solution = solutionRepository.findById(id)
                    .orElseThrow(() -> {
                        log.error("未找到解决方案，ID: {}", id);
                        return new RuntimeException("Solution not found with id: " + id);
                    });
            log.info("成功获取解决方案详情，ID: {}", id);
            return solution;
        } catch (Exception e) {
            log.error("获取解决方案详情失败，ID: {}", id, e);
            throw e;
        }
    }

    @Override
    @Transactional
    public Solution createSolution(Solution solution) {
        log.debug("开始创建解决方案，方案名称: {}", solution.getSolutionName());
        try {
            Solution savedSolution = solutionRepository.save(solution);
            log.info("成功创建解决方案，ID: {}, 方案名称: {}", savedSolution.getId(), savedSolution.getSolutionName());
            return savedSolution;
        } catch (Exception e) {
            log.error("创建解决方案失败，方案名称: {}", solution.getSolutionName(), e);
            throw e;
        }
    }

    @Override
    @Transactional
    public Solution updateSolution(Integer id, Solution solution) {
        log.debug("开始更新解决方案，ID: {}", id);
        try {
            Solution existingSolution = getSolutionById(id);
            log.debug("更新解决方案字段，ID: {}", id);
            existingSolution.setSolutionName(solution.getSolutionName());
            existingSolution.setUserGroup(solution.getUserGroup());
            existingSolution.setDescription(solution.getDescription());
            existingSolution.setContent(solution.getContent());
            existingSolution.setDocumentationUrl(solution.getDocumentationUrl());
            
            Solution updatedSolution = solutionRepository.save(existingSolution);
            log.info("成功更新解决方案，ID: {}", id);
            return updatedSolution;
        } catch (Exception e) {
            log.error("更新解决方案失败，ID: {}", id, e);
            throw e;
        }
    }

    @Override
    @Transactional
    public void deleteSolution(Integer id) {
        log.debug("开始删除解决方案，ID: {}", id);
        try {
            solutionRepository.deleteById(id);
            log.info("成功删除解决方案，ID: {}", id);
        } catch (Exception e) {
            log.error("删除解决方案失败，ID: {}", id, e);
            throw e;
        }
    }
} 